package com.jp.juc.thread;

/**
 * 打断
 */
@SuppressWarnings("CallToPrintStackTrace")
public class UseInterrupt {


    public static void main(String[] args) {
        Thread thread = new Thread(UseInterrupt::interruptTest, "A");

        thread.start();
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 打断
        thread.interrupt();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
    }


    public static void interruptTest() {


        while (true) {
            System.out.println("do---do");
            /*try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // 响应中断 抛出异常后会立即将线程的中断标示位清除
                System.out.println("==sleep interrupt==");
                e.printStackTrace();
            }*/
            // 睡眠中断后又会重新执行 调用以下方法 返回 false
            // Thread.interrupted();

            // 响应中断
            if (Thread.currentThread().isInterrupted()) {
                System.out.println("==exit==");
                System.out.println("Thread.currentThread().isInterrupted() = " + Thread.currentThread().isInterrupted());
                // 退出
                break;
            }

        }
    }


}
